# coding=utf-8
from appium.webdriver.common.appiumby import AppiumBy as By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from pages.base_page import BasePage


class UploadPage(BasePage):
    # === 3. 元素定位器（集中管理，便于维护） ===
    upload_locator = (By.ID, 'com.xiachufang:id/iv_home_upload')  # 主页上传按钮
    pass_recipe_locator = (By.ANDROID_UIAUTOMATOR, 'new UiSelector().text("传菜谱")')  # 传菜谱按钮
    # 菜谱照片
    photo_recipe_locator = (
        By.ANDROID_UIAUTOMATOR, 'new UiSelector().resourceId("com.xiachufang:id/item_photo_picker_photo").instance(1)')
    recipe_name = (By.ID, 'com.xiachufang:id/et_recipe_name')  # 菜谱标题输入
    # 菜谱材料编辑
    et_cooking_locator = (By.ANDROID_UIAUTOMATOR, 'new UiSelector().resourceId("com.xiachufang:id/et_ing").instance(0)')
    et_cooking_amount = (By.ID, 'com.xiachufang:id/et_amount')  # 材料用量
    # 菜谱图步骤按钮
    recipe_step_locator = (By.ID, 'com.xiachufang:id/tv_step_hint')
    # 步骤照片
    photo_step_locator = (
        By.ANDROID_UIAUTOMATOR, 'new UiSelector().resourceId("com.xiachufang:id/item_photo_picker_photo").instance(1)')
    # 发布菜谱按钮
    publish_recipe_locator = (By.ID, 'com.xiachufang:id/tv_publish')
    # 发布成功文字
    publish_success_tips = (By.ID, 'com.xiachufang:id/model_create_success_textview')

    pass_work_locator = (By.ANDROID_UIAUTOMATOR, 'new UiSelector().text("传作品")')  # 传作品按钮
    # 作品照片
    photo_work_locator = (
        By.ANDROID_UIAUTOMATOR, 'new UiSelector().resourceId("com.xiachufang:id/item_photo_picker_photo").instance(1)')
    next_step_locator = (By.ID, 'com.xiachufang:id/tv_next_step')  # 下一步按钮
    work_content_locator = (By.ID, 'com.xiachufang:id/dish_desc')  # 作品内容
    upload_work_locator = (By.ID, 'com.xiachufang:id/dish_update_image')  # 发布按钮
    upload_work_success_tips = (By.ID, 'com.xiachufang:id/navigation_bar_title_layout')  # 出现”发送成功消息“

    remember_diet_locator = (By.ANDROID_UIAUTOMATOR, 'new UiSelector().text("记饮食")')  # 记饮食按钮
    breakfast_add_locator = (By.XPATH, "//android.view.View[contains(@content-desc, '早餐')]")  # 早餐按钮
    egg_option_locator = (By.XPATH, "//android.view.View[contains(@content-desc, '热量 139 千卡')]")  # 鸡蛋选项按钮
    ok_locator = (By.ANDROID_UIAUTOMATOR, 'new UiSelector().description("确定")')  # 确定按钮
    finish_locator = (By.ANDROID_UIAUTOMATOR, 'new UiSelector().description("完成")')  # 完成按钮
    remember_diet_success_tips_locator = (By.XPATH, "//*[contains(@text,'饮食记录成功')]")  # ”饮食记录成功“toast
    total_heat_locator = (
        By.ANDROID_UIAUTOMATOR, 'new UiSelector().descriptionMatches(".*\\d+.*")')

    # === 页面操作方法 ===
    def __init__(self, driver, timeout=10):
        super().__init__(driver, timeout)
        self.logger = None

    def click_upload(self):
        """点击主页上传按钮"""
        self.click(self.upload_locator)

    def click_pass_recipe(self):
        """点击【传菜谱】按钮"""
        self.click(self.pass_recipe_locator)

    def select_photo_recipe(self):
        """选择一张菜谱照片"""
        self.click(self.photo_recipe_locator)

    def input_recipe_name(self, name: str):
        """输入菜谱名称"""
        self.send_keys(self.recipe_name, name)

    def input_cooking_material(self, material: str):
        """输入菜谱用料"""
        self.send_keys(self.et_cooking_locator, material)

    def input_cooking_amount(self, amount: int):
        """输入材料用量"""
        self.send_keys(self.et_cooking_amount, amount)

    def click_recipe_step_plus(self):
        """点击【菜谱步骤】按钮"""
        self.click(self.recipe_step_locator)

    def select_photo_step(self):
        """选择步骤照片"""
        self.click(self.photo_step_locator)

    def click_publish_recipe(self):
        """点击发布菜谱按钮"""
        self.click(self.publish_recipe_locator)

    def get_publish_success_tips(self):
        """获取发布成功提示文字"""
        return self.get_text(self.publish_success_tips)

    def click_pass_work(self):
        """点击【传作品】按钮"""
        self.click(self.pass_work_locator)

    def select_photo_work(self):
        """选择作品照片"""
        self.click(self.photo_work_locator)

    def click_next_step(self):
        """点击下一步按钮"""
        self.click(self.next_step_locator)

    def input_work_content(self, content: str):
        """输入作品内容"""
        self.send_keys(self.work_content_locator, content)

    def click_upload_work(self):
        """点击上传作品按钮"""
        self.click(self.upload_work_locator)

    def get_upload_work_success_tips(self):
        """获取作品上传成功提示"""
        return self.get_text(self.upload_work_success_tips)

    def click_remember_diet(self):
        """点击【记饮食】按钮"""
        self.click(self.remember_diet_locator)

    def click_breakfast_add(self):
        """点击早餐按钮"""
        self.click(self.breakfast_add_locator)

    def select_egg(self):
        """选择鸡蛋食材"""
        self.click(self.egg_option_locator)

    def click_ok(self):
        """点击确定按钮"""
        self.click(self.ok_locator)

    def click_finish(self):
        """点击完成按钮"""
        self.click(self.finish_locator)

    def get_remember_diet_success_tips(self):
        """获取饮食记录成功"""
        self.get_text(self.remember_diet_success_tips_locator, 3)

    def get_toast(self, message="饮食记录成功", timeout=5):
        """
        捕获 Toast 提示
        :param message: toast
        :param timeout: 超时时间
        :return: toast 文本
        """
        try:
            # Toast 通常用 text 属性
            locator = (By.XPATH, f"//*[contains(@text, '{message}')]")
            element = WebDriverWait(self.driver, timeout, 0.1).until(
                EC.presence_of_element_located(locator)
            )
            return element.get_attribute("text")
        except Exception:
            raise Exception("未捕获到 Toast")

    def test_toast(self):
        # 获取toast提示框内容
        toast_element = WebDriverWait(self.driver, 5).until(
            lambda x: x.find_element(By.XPATH, "//*[@text='饮食记录成功']"))
        print(toast_element.text)
        return toast_element.text

    def get_total_heat(self) -> float:
        """得到热量"""
        heat_element = self.find_element(self.total_heat_locator)
        heat_value = float(heat_element.get_attribute("content-desc"))
        return heat_value



